﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla.Core;
using Csla.Data;

namespace Accounting.Library.GhiSo
{
    public enum DongTien { Vao, Ra, ChuaXacDinh }
    public class GhiSoService
    {
        #region Fields in SoChiTiet Table
        protected System.Nullable<int> Khoa = 0;
        protected System.Nullable<int> LoaiChungTuId = 0;
        protected System.Nullable<int> PhuongThucThanhToanId = 0;
        protected System.Nullable<int> NhaCungCapId = 0;
        protected System.Nullable<int> ChungTuGocId = 0;
        protected double TongTienThanhToanQĐ = 0;
        protected double TongTienThanhToan = 0;

        protected DateTime? NgayChungTu = DateTime.Now;
        protected DateTime? NgayHachToan = DateTime.Now;
        protected String SoHD = null;
        protected System.Nullable<int> KyKetChuyenId = 0;
        protected DongTien dongTien = DongTien.ChuaXacDinh;
        #endregion

        protected object ChungTu;

        public GhiSoService(object ChungTu)
        {
            this.ChungTu = ChungTu;
            LoadThongTinChung();
        }

        public virtual void ButToan(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx)
        {
            CapNhatCongNoKhachHang(ctx);
            GhiSoChiTiet(ctx);
        }

        protected virtual void LoadThongTinChung()
        {
        }

        public virtual void GhiSoChiTiet(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx)
        {

        }

#if !SILVERLIGHT
        public static bool DaGhiSo(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int LoaiChungTuId, int ChungTuGocId)
        {
            //get MSP_MuaHangHoa data
            int numOfRows = (from p in ctx.DataContext.TH_SoChiTiets
                             where (p.LoaiChungTuId == LoaiChungTuId && p.ChungTuGocId == ChungTuGocId)
                             select 1).Count();

            if (numOfRows > 0)
                return true;
            else
                return false;
        }

        protected void GhiSoTaiKhoanNgoaiBang(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int? LoaiTienId)
        {
            int maTKNgoaiTe = LoaiTienTeService.MaTaiKhoanNgoaiTe(ctx, (int)LoaiTienId);
            string DienGiai = "Ghi sổ tài khoản ngoại bảng của hóa đơn: " + SoHD;
            int TkNoId = 0;
            int TkCoId = 0;
            if (dongTien == DongTien.Vao)
            {
                TkNoId = maTKNgoaiTe;
            }
            else if (dongTien == DongTien.Ra)
            {
                TkCoId = maTKNgoaiTe;
            }
            ctx.DataContext.addTH_SoChiTiet(ref Khoa, ChungTuGocId, 0, LoaiChungTuId, NgayChungTu, NgayHachToan, SoHD, TkNoId, TkCoId, 
                DienGiai, TongTienThanhToan, KyKetChuyenId, BusinessConfiguration.Configuration.NGOAIBANG, null, null);
        }

        protected virtual void CapNhatCongNoKhachHang(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx)
        {
            if (BusinessConfiguration.Configuration.LaChungTuGhiTangCongNo((int)LoaiChungTuId))
            {
                if (PhuongThucThanhToanService.ThanhToanBangCongNo(ctx, (int)this.PhuongThucThanhToanId))
                {
                    NhaCungCapService.GhiTangCongNo(ctx, (int)this.NhaCungCapId, (double)this.TongTienThanhToanQĐ);
                }
            }
            else if (BusinessConfiguration.Configuration.LaChungTuGhiGiamCongNo((int)LoaiChungTuId))
            {
                NhaCungCapService.GhiGiamCongNo(ctx, (int)this.NhaCungCapId, (double)this.TongTienThanhToanQĐ);
            }
            else if (BusinessConfiguration.Configuration.LaChungTuGhiTangKyGui((int)LoaiChungTuId))
            {
                NhaCungCapService.GhiTangKyGui(ctx, (int)this.NhaCungCapId, (double)this.TongTienThanhToanQĐ);
            }
            else if (BusinessConfiguration.Configuration.LaChungTuGhiGiamKyGui((int)LoaiChungTuId))
            {
                NhaCungCapService.GhiGiamKyGui(ctx, (int)this.NhaCungCapId, (double)this.TongTienThanhToanQĐ);
            }
        }
#endif
    }
}